package com.applink.sell_proof;

import java.util.Observer;

import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager.SimpleOnPageChangeListener;
import android.view.KeyEvent;
import android.widget.CompoundButton;

import com.actionbarsherlock.view.MenuItem;
import com.applink.sell_proof.api.DataHelper;
import com.applink.sell_proof.fragment.AllProjectFragment;
import com.applink.sell_proof.fragment.SearchProject;
import com.applink.sell_proof.fragment.adapter.BaseFragmentPagerAdapter;
import com.applink.sell_proof.fragment.adapter.SellFragmentPagerAdapter;

public class AllProjectActivity extends BasePagerActivity {

	protected BaseFragmentPagerAdapter mFragmentAdapter;

	public static int LOADER_DRAFT_ID = 0;
	public static int LOADER_SUBMITTED_ID = 1;
	public static int LOADER_VALIDATED_ID = 2;

	@Override
	protected void onCreate(Bundle saveInstanceState) {
		super.onCreate(saveInstanceState);
		setTitle(getString(R.string.project));
		addSimpleOnPageChangeListener(mOnPageChangeListener);
		new Handler().postDelayed(new Runnable() {
			
			@Override
			public void run() {
				//new loadData().execute();				
			}
		}, 5000);
		
	}

	@Override
	protected BaseFragmentPagerAdapter getFragmentPagerAdapter() {
		if (mFragmentAdapter == null) {
			mFragmentAdapter = new SellFragmentPagerAdapter(
					getSupportFragmentManager());
		}
		return mFragmentAdapter;
	}

	@Override
	protected int getDefaultPagerIndex() {
		return 0;
	}

	@Override
	public void setSelectedTab() {
		((CompoundButton) findViewById(R.id.deal_tab)).setChecked(true);
	}

	@Override
	public int getActivityIndex() {
		return BasePagerActivity.SELL_ACTIVITY;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if (item.getItemId() == R.id.search_bar) {

			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public boolean onKeyUp(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			Fragment searchResult = getSupportFragmentManager()
					.findFragmentByTag(SearchProject.TAG);
			if (searchResult != null && searchResult.isVisible()) {
				FragmentTransaction transaction = getSupportFragmentManager()
						.beginTransaction();
				transaction.remove(searchResult);
				transaction.commit();
				return true;
			}
		}
		return super.onKeyUp(keyCode, event);
	}

	private SimpleOnPageChangeListener mOnPageChangeListener = new SimpleOnPageChangeListener() {

		public void onPageSelected(int position) {
			try {
				DataHelper.getInstance().addObserver(
						(Observer) getFragmentAt(position));

				((AllProjectFragment) getFragmentAt(position)).getOpportunity();
			} catch (Exception e) {

			}
		}
	};

//	private class loadData extends AsyncTask<Void, Void, Integer> {
//
//		@Override
//		protected Integer doInBackground(Void... params) {
//	
//					if (PriceBookDBHelper.getAllPriceBook() == null
//							|| PriceBookDBHelper.getAllPriceBook().size() == 0) {
//
//						DataHelper.getInstance().getPriceBook();
//					}
//
//					if (PromotionDBHelper.getAllPromotion() == null
//							|| PromotionDBHelper.getAllPromotion().size() == 0) {
//
//						DataHelper.getInstance().getPromotion();
//					}			
//			
//			return 0;
//		}
//
//		@Override
//		protected void onPostExecute(Integer result) {
//			super.onPostExecute(result);
//			// for(int i =0 ;i< 2;i++){
//			// showNotification(i, mContext);
//			// }
//
//		}
//
//	}

	// private void showNotification(int id,Context mContext) {
	// final NotificationManager mNotification =
	// (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
	// Notification note = new Notification(R.drawable.new_icon,
	// "New Message Dell App", System.currentTimeMillis());
	// note.flags |= Notification.FLAG_AUTO_CANCEL |
	// Notification.FLAG_SHOW_LIGHTS;
	// if(id == 0){
	// PendingIntent i = PendingIntent.getActivity(getApplicationContext(), 0,
	// new Intent(
	// getApplicationContext(), PriceBookActivity.class), 0);
	// note.setLatestEventInfo(getApplicationContext(), "Attention",
	// "Please check the new pricebook", i);
	// }else if (id == 1){
	// PendingIntent ii = PendingIntent.getActivity(getApplicationContext(), 0,
	// new Intent(
	// getApplicationContext(), Promotion.class), 0);
	// note.setLatestEventInfo(getApplicationContext(), "Attention",
	// "Please check the new promotion", ii);
	// }
	//
	// mNotification.notify(id, note);
	// //mNotification.cancel(id);
	// // mgr.notify(1,note);
	//
	// }
}
